package com.redsoft.spirit.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

public class CodeTable implements Map, Serializable {
	private Hashtable ihstbl_CodeTable = new Hashtable();
	private List ilist_Keys = new Vector();

	public int getItemCount() {
		return this.ihstbl_CodeTable.size();
	}

	/**
	 * @deprecated
	 */
	public String getDisplayValue(String as_CodeValue) {
		return getDisplay(as_CodeValue);
	}

	public String getDisplay(Enum aenum_CodeValue) {
		return getDisplay(aenum_CodeValue.toString());
	}

	public String getDisplay(String as_CodeValue) {
		String ls_CodeValue = CommonUtil.trim(as_CodeValue);

		if (this.ihstbl_CodeTable.containsKey(ls_CodeValue)) {
			return (String) this.ihstbl_CodeTable.get(ls_CodeValue);
		}

		if (CommonUtil.isNumber(ls_CodeValue)) {
			double ldbl_Value = CommonUtil.parseDouble(ls_CodeValue);
			Enumeration lenum_Keys = this.ihstbl_CodeTable.keys();
			while (lenum_Keys.hasMoreElements()) {
				String ls_Key = (String) lenum_Keys.nextElement();
				if (!CommonUtil.isNumber(ls_Key))
					continue;
				double ldbl_Key = CommonUtil.parseDouble(ls_Key);
				if (ldbl_Value == ldbl_Key)
					return (String) this.ihstbl_CodeTable.get(ls_Key);
			}
		}
		return ls_CodeValue;
	}

	public String getDisplay(long al_CodeValue) {
		return getDisplayValue(Long.toString(al_CodeValue));
	}

	public String[] getDataValues() {
		String[] larrs_Return = new String[this.ilist_Keys.size()];
		for (int i = 0; i < this.ilist_Keys.size(); i++) {
			larrs_Return[i] = CommonUtil.trim((String) this.ilist_Keys.get(i));
		}
		return larrs_Return;
	}

	public int insertItem(long al_Value, String as_Display, int ai_Index) {
		return insertItem(Long.toString(al_Value), as_Display, ai_Index);
	}

	public int insertItem(int ai_Value, String as_Display) {
		return insertItem(Integer.toString(ai_Value), as_Display, 0);
	}

	public int insertItem(String as_Value, String as_Display) {
		return insertItem(CommonUtil.trim(as_Value), as_Display, 0);
	}

	public int insertItem(Enum aenum_Value, String as_Display) {
		return insertItem(aenum_Value.toString(), as_Display, 0);
	}

	public int insertItem(String as_Value, String as_Display, int ai_Index) {
		String ls_Value = CommonUtil.trim(as_Value);
		String ls_Display = as_Display;
		if (ls_Display == null)
			ls_Display = "";

		int li_Index = ai_Index;
		if (li_Index < 0)
			li_Index = 1;
		if ((li_Index > this.ilist_Keys.size() + 1) || (li_Index == 0)) {
			li_Index = this.ilist_Keys.size() + 1;
		}

		if (this.ihstbl_CodeTable.containsKey(ls_Value))
			return -1;

		this.ilist_Keys.add(li_Index - 1, as_Value);
		this.ihstbl_CodeTable.put(ls_Value, ls_Display);

		return 1;
	}

	public int removeItem(String as_Value) {
		String ls_Value = CommonUtil.trim(as_Value);
		if (!this.ilist_Keys.contains(ls_Value))
			return 0;

		this.ilist_Keys.remove(ls_Value);
		this.ihstbl_CodeTable.remove(ls_Value);

		return 1;
	}

	public int removeItem(long al_Value) {
		return removeItem(Long.toString(al_Value));
	}

	public boolean containsValue(long al_Val) {
		return containsValue(Long.toString(al_Val));
	}

	protected void printMsg(String as_Msg) {
		System.out.println(getClass().getName() + ":" + as_Msg);
	}

	protected void printMsg(String as_MethodName, String as_Msg) {
		System.out.println(getClass().getName() + "." + as_MethodName + " : " + as_Msg);
	}

	public void clear() {
		this.ilist_Keys.clear();
		this.ihstbl_CodeTable.clear();
	}

	public boolean containsKey(Object aobj_Key) {
		return this.ilist_Keys.contains(aobj_Key);
	}

	public boolean containsValue(Object value) {
		return this.ihstbl_CodeTable.containsValue(value);
	}

	public boolean containsValue(String value) {
		String ls_Value = CommonUtil.trim(value);
		if (this.ilist_Keys.contains(ls_Value))
			return true;

		if ((this.ihstbl_CodeTable != null) && (this.ihstbl_CodeTable.containsValue(ls_Value)))
			return true;

		if (CommonUtil.isNumber(ls_Value)) {
			double ldbl_Value = CommonUtil.parseDouble(ls_Value);
			Enumeration lenum_Keys = this.ihstbl_CodeTable.keys();
			while (lenum_Keys.hasMoreElements()) {
				String ls_Key = (String) lenum_Keys.nextElement();
				if (!CommonUtil.isNumber(ls_Key))
					continue;
				double ldbl_Key = CommonUtil.parseDouble(ls_Key);
				if (ldbl_Value == ldbl_Key)
					return true;
			}
		}
		return false;
	}

	public Set entrySet() {
		return null;
	}

	public Object get(Object key) {
		return this.ihstbl_CodeTable.get(key);
	}

	public boolean isEmpty() {
		return this.ihstbl_CodeTable.isEmpty();
	}

	public Set keySet() {
		Set lset_S = new HashSet();
		for (int i = 0; i < this.ilist_Keys.size(); i++) {
			lset_S.add(this.ilist_Keys.get(i));
		}
		return lset_S;
	}

	public Object put(Object key, Object value) {
		if (key == null) {
			printMsg("put()", "key is null");
			return null;
		}
		if (value == null) {
			printMsg("put()", "value is null");
			return null;
		}
		insertItem(key.toString(), value.toString(), 0);
		return value;
	}

	public void putAll(Map t) {
		if (t == null) {
			return;
		}

		Set lset_Keys = t.keySet();
		Iterator lit_Keys = lset_Keys.iterator();
		while (lit_Keys.hasNext()) {
			Object lobj_Key = lit_Keys.next();
			if (this.ihstbl_CodeTable.containsKey(lobj_Key))
				continue;
			this.ilist_Keys.add(lobj_Key);
			this.ihstbl_CodeTable.put(lobj_Key, t.get(lobj_Key));
		}
	}

	public Object remove(Object key) {
		Object lobj_Value = null;
		if (this.ihstbl_CodeTable.containsKey(key)) {
			lobj_Value = this.ihstbl_CodeTable.get(key);
			this.ilist_Keys.remove(key);
			this.ihstbl_CodeTable.remove(key);
		}
		return lobj_Value;
	}

	public int size() {
		return this.ihstbl_CodeTable.size();
	}

	public Collection values() {
		return this.ihstbl_CodeTable.values();
	}
}